package com.hwd.srb.core.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author 黄伟东/Victor
 * @date 2022/3/21 22:08
 * @Description
 */
@Aspect
@Component
@Slf4j
public class LogAspect {

    @Pointcut("execution(public * *..*Controller.*(..))")
    public void pt1() {
    }

    @Around("pt1()")
    public Object totalTime(ProceedingJoinPoint proceedingJoinPoint) {

        String methodName = proceedingJoinPoint.getSignature().getName();

        //执行目标方法之前
        log.info("[环绕通知] " + methodName + " 方法开始执行");

        long startTime = System.currentTimeMillis();

        //执行目标方法
        Object result = null;

        try {
            result = proceedingJoinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        //执行目标方法之后
        long endTime = System.currentTimeMillis();

        log.info("[环绕通知] " + methodName + " 方法执行完毕，执行时长为：" + (endTime - startTime) + " 毫秒");

        return result;
    }
}
